<?php

namespace Lib;

final class Queue {

    /**
     * 压入队列
     *
     * @param string $name
     * @param array  $data
     *
     * @return boolean
     */
    public static function push($name, array $data) {
        $key   = sprintf(\Lib\Consts::QUEUE, $name);
        $reids = \Yaf\Registry::get('redis');

        $reids->rPush($key, \json_encode($data));
        $reids->expire($key, \Lib\Consts::EXPIRE_7DAY);

        return true;
    }

    /**
     * 弹出队列
     *
     * @param string $name
     * @return array
     */
    public static function pop($name) {
        $key  = sprintf(\Lib\Consts::QUEUE, $name);
        $json = \Yaf\Registry::get('redis')->blPop($key, 10);
        $data = \json_decode($json, true);

        return (JSON_ERROR_NONE === json_last_error() ? $data : []);
    }
}

